﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: WebDeployParameterViewModel
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using VmroleAuthor.Models;

namespace VmroleAuthor.ViewModels
{
    public class WebDeployParameterViewModel : ViewModelBase, IDataErrorInfo, ICleanupParamsAndPayload
    {                    
        private readonly WebDeploymentParameters webDeploymentParameters;
        private readonly VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel;
        private StringParameterizableProperty webDeployParameterValue;

        public static class PropertyNames
        {
            public const string ParameterName = "ParameterName";
            public const string ParameterValue = "ParameterValue";          
        }       

        /// <summary>
        /// Constructor
        /// </summary>
        public WebDeployParameterViewModel(WebDeploymentParameters webDeploymentParameters, VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel)
        {
            if (webDeploymentParameters == null)
            {
                webDeploymentParameters = new WebDeploymentParameters();
            }
            this.webDeploymentParameters = webDeploymentParameters;
            this.vmRoleResourceExtensionViewModel = vmRoleResourceExtensionViewModel;           
        }

        public WebDeploymentParameters WebDeploymentParameters
        {
            get { return this.webDeploymentParameters; }            
        }

        public string ParameterName
        {
            get
            {
                if (string.IsNullOrEmpty(this.webDeploymentParameters.ParameterName))
                {
                    return "Enter New WebDeploy Parameter Name";
                }
                return this.webDeploymentParameters.ParameterName;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    this.webDeploymentParameters.ParameterName = value;                  
                }
                this.OnPropertyChanged(PropertyNames.ParameterName);
                this.OnPropertyChanged(PropertyNames.ParameterValue);
            }
        }

        public List<string> AvailableParameters
        {
            get { return this.vmRoleResourceExtensionViewModel.ResourceExtensionParametersViewModel.AvailableParameters; }
        }

        public StringParameterizableProperty ParameterValue
        {
            get
            {
                if (this.webDeployParameterValue == null)
                {
                    Func<string> paramValueGetter = () =>
                        {
                            if (this.webDeploymentParameters.Value == null)
                            {
                                return "Enter WebDeploy Parameter Value";
                            }
                            return this.webDeploymentParameters.Value;
                        };
                    Action<string> paramValueSetter = delegate(string value) { this.webDeploymentParameters.Value = value; };
                    Func<string> autoParamHintGetter = () => (this.webDeploymentParameters.ParameterName ?? string.Empty) + PropertyNames.ParameterValue;
                    ViewDefParamInfo viewDefParamInfo = new ViewDefParamInfo
                    {
                        ViewDefParameterType = ViewDefParameterType.String,
                        DefaultEnglishLabelText = "Web deploy parameter"
                    };


                    this.webDeployParameterValue = new StringParameterizableProperty(
                        PropertyNames.ParameterValue,
                        paramValueGetter,
                        paramValueSetter,
                        autoParamHintGetter,
                         ParameterType.String,
                        "Web deploy parameter",
                         this.vmRoleResourceExtensionViewModel.ResourceExtensionParametersViewModel,
                         true,
                         viewDefParamInfo
                        );
                }
                return this.webDeployParameterValue;
            }
        }

        public void Cleanup()
        {
            this.ParameterValue.Cleanup();
        }

        public string this[string propertyName]
        {
            get
            {
                string error = null;
                switch (propertyName)
                {
                    case PropertyNames.ParameterName:
                        if (string.IsNullOrEmpty(this.webDeploymentParameters.ParameterName))
                        {
                            error = "WebDeploy Parameter Name must be specified";
                        }
                        else
                        {
                            error = ParameterizationHelper.ValidateCannotBeParameterizedBySpecification(this.ParameterName, true);
                        }
                        
                        break;  
                    case PropertyNames.ParameterValue:
                        if (this.webDeploymentParameters.Value == null)
                        {
                            error =  "WebDeploy Parameter Value must be specified";
                        }
                        break;
                }
                return error;
            }
        }

        public string Error { get { return null; } }
        
    }
}
